﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Skewworks.Win.Motif.Glide
{
    public partial class GlidePasswordBox : UserControl, IEmbeddedControl
    {

        #region Variables

        private string _text;
        private string _pwdText;
        private GlideFonts _font;
        private StringAlignment _ha;
        private Color _foreColor;

        private ISettings _settings;

        #endregion

        public GlidePasswordBox()
        {
            InitializeComponent();

            _font = GlideFonts.Droid_12;
            this.Font = GlideFont.DroidSans(12);
            _ha = StringAlignment.Near;
            _foreColor = Color.Black;
            _settings = new GlidePasswordBoxSettings(this);
            this.Width = 80;
            this.Height = 32;
            UpdatePwdText();
        }

        #region Propeties

        public override string Text
        {
            get { return _text; }
            set { _text = value; UpdatePwdText(); Invalidate(); }
        }

        public StringAlignment TextAlign
        {
            get { return _ha; }
            set { _ha = value; Invalidate(); }
        }

        public GlideFonts GFont
        {
            get { return _font; }
            set
            {
                _font = value;
                this.Font = GlideFont.DroidSans((int)value);
                Invalidate();
            }
        }

        public Color FontColor
        {
            get { return _foreColor; }
            set { _foreColor = value; Invalidate(); }
        }

        public ISettings Settings
        {
            get { return _settings; }
        }

        #endregion

        #region GUI

        protected override void OnPaint(PaintEventArgs e)
        {
            lock (this)
            {
                e.Graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, Width, Height));
                e.Graphics.DrawRectangle(Pens.Gray, new Rectangle(0, 0, Width - 1, Height - 1));

                StringFormat SF = new StringFormat();
                SF.Alignment = _ha;
                e.Graphics.DrawString(_pwdText, this.Font, new SolidBrush(_foreColor), new Rectangle(0, this.Height / 2 - this.Font.Height / 2, this.Width, this.Font.Height), SF);
                if (Focused)
                {
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(0, 0, Width - 1, Height - 1));
                    e.Graphics.DrawRectangle(Pens.Orange, new Rectangle(1, 1, Width - 3, Height - 3));
                }
            }
        }

        private void UpdatePwdText()
        {
            _pwdText = string.Empty;
            if (_text == null)
                return;
            while (_pwdText.Length < _text.Length)
                _pwdText += "*";
        }

        #endregion

        #region Moving/Sizing

        private bool _sizeV, _sizeH, _move;
        private Point _sizeRef;

        private void GlidePasswordBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (_sizeH || _sizeV)
            {
                if (_sizeH)
                    this.Width += e.X - _sizeRef.X;
                if (_sizeV)
                    this.Height += e.Y - _sizeRef.Y;
                Invalidate();
            }
            else if (_move)
            {
                this.Left += e.X - _sizeRef.X;
                this.Top += e.Y - _sizeRef.Y;
                return;
            }
            else
            {
                if (e.X >= this.Width - 8)
                {
                    if (e.Y >= this.Height - 8)
                        this.Cursor = Cursors.SizeNWSE;
                    else
                        this.Cursor = Cursors.SizeWE;
                }
                else if (e.Y >= this.Height - 8)
                    this.Cursor = Cursors.SizeNS;
                else
                    this.Cursor = Cursors.Default;
            }
            _sizeRef = new Point(e.X, e.Y);
        }

        private void GlidePasswordBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= this.Width - 8)
            {
                if (e.Y >= this.Height - 8)
                {
                    _sizeH = true;
                    _sizeV = true;
                }
                else
                    _sizeH = true;
            }
            else if (e.Y >= this.Height - 8)
                _sizeV = true;
            else
            {
                _move = true;
                this.Cursor = Cursors.SizeAll;
            }
        }

        private void GlidePasswordBox_MouseUp(object sender, MouseEventArgs e)
        {
            _sizeH = false;
            _sizeV = false;
            _move = false;
            this.Cursor = Cursors.Default;
        }

        #endregion

    }
}
